package com.payment.ktb.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.payment.ktb.R;
import com.payment.ktb.activity.main1.AddBankcardActivity;
import com.payment.ktb.adapter.ConfirmOrderCardAdapter;
import com.payment.ktb.model.BankCard;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by syd on 2016/6/10.
 */
public class CardsDialog extends Dialog {

    private Context mContext;
    private List<BankCard> listData = new ArrayList<BankCard>();
    private List<BankCard> top5ListData = new ArrayList<BankCard>();
    private Window window = null;
    private ConfirmOrderCardAdapter mConfirmOrderCardAdapter;
    private OnCardDialogClickListener mOnCardDialogClickListener;
    ListView listView;

    public CardsDialog(Context context, List<BankCard> listData, OnCardDialogClickListener mOnCardDialogClickListener) {
        super(context);
        this.mContext = context;
        this.listData = listData;
        this.mOnCardDialogClickListener = mOnCardDialogClickListener;
        initTop5ListData();
        showDialog();
    }



    private void initTop5ListData() {
        top5ListData.clear();
        if (listData != null && listData.size() > 0 && listData.size() <= 5) {
            for (BankCard bankCard : listData) {
                top5ListData.add(bankCard);
            }
        } else if (listData != null && listData.size() > 5){
            for (int i = 0; i <5 ; i++) {
                top5ListData.add(listData.get(i));
            }
        }

    }


    private void showDialog() {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_confirm_order);
        windowDeploy();
        setCanceledOnTouchOutside(true);
        listView = (ListView) findViewById(R.id.dialog_confirm_order_list_view);
        mConfirmOrderCardAdapter = new ConfirmOrderCardAdapter(mContext,top5ListData);
        View footer = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm_order_footer, null);
        footer.findViewById(R.id.dialog_confirm_order_footer_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext,AddBankcardActivity.class));
                dismiss();
            }
        });
        listView.addFooterView(footer);
        View header = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm_order_header,null);
        EditText editText = (EditText) header.findViewById(R.id.et_confirm_order_header_search);
        editText.addTextChangedListener(textWatcher);
        listView.addHeaderView(header);
        listView.setAdapter(mConfirmOrderCardAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mOnCardDialogClickListener.onCardDialogItemClickListener(top5ListData.get(position-1).getBankNo(),top5ListData.get(position-1).getBankName());
                dismiss();
            }
        });
        show();
    }




    private TextWatcher textWatcher = new TextWatcher() {



        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String etValues = s.toString();
            if (TextUtils.isEmpty(etValues)) {
                initTop5ListData();
            } else {
                searchBankCardByLast4Num(etValues);
            }
            mConfirmOrderCardAdapter.notifyDataSetChanged();

        }

    };


    /**
     * 根据银行卡后4位查找符合的银行卡
     */
    private void searchBankCardByLast4Num(String last4BankCardNums) {
        top5ListData.clear();
        for (BankCard bankcard : listData) {
            if (bankcard.getBankNo().substring(bankcard.getBankNo().length() - 4, bankcard.getBankNo().length()).contains(last4BankCardNums)) {
                top5ListData.add(bankcard);
            }
        }


    }


    public void windowDeploy() {
        window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim);
//        window.setBackgroundDrawableResource(R.color.transparent);
    }



    public interface OnCardDialogClickListener{
        void onCardDialogItemClickListener(String cardNo, String cardBank);
    }

}
